import utils from '@/lib/utils';
import { HTTP_CODE } from "../http/val";
import message from '../message';
interface IconnectParams {
    openFunc?: any;
    successMessage?: any;
    errorMessage?: any;
    errorFunc?: any;
    closeFunc?: any;
}
const connectParams = {
    openFunc: null,
    successMessage: null,
    errorMessage: null,
    errorFunc: null,
    closeFunc: null
};
/*
Socket.readyState
0 - 表示连接尚未建立。
1 - 表示连接已建立，可以进行通信。
2 - 表示连接正在进行关闭。
3 - 表示连接已经关闭或者连接不能打开。
*/
export default class PSocket {
    private host: string = "";
    private socket!: WebSocket;
    private socketType: string = "";
    constructor(host: string, url: string) {
        this.host = `ws://${host}/ws${url}`;
    }

    public Connect({ openFunc = null, successMessage = null, errorMessage = null, errorFunc = null, closeFunc = null }: IconnectParams = connectParams) {
        // 实例化socket
        this.socket = new WebSocket(this.host);
        // 监听socket连接
        this.socket.onopen = (e: any) => {
            // this.socket.send("监听socket连接");
            this.socketType = e.type;
            if (!utils.empty(openFunc)) {
                openFunc(this, e);
            }
        };
        // 监听socket错误信息
        this.socket.onerror = (e: any) => {
            this.socketType = e.type;
            if (!utils.empty(errorFunc)) {
                errorFunc(this, e);
            }
        };
        // 销毁监听
        this.socket.onclose = (e: any) => {
            this.socketType = e.type;
            if (!utils.empty(closeFunc)) {
                closeFunc(this, e);
            }
        };
        // 监听socket消息
        this.socket.onmessage = (msg: any) => {
            // console.log("收到消息：", msg.data);
            const data: any = JSON.parse(msg.data);
            switch (data.code) {
                case HTTP_CODE.SUCCESS:
                    if (!utils.empty(successMessage)) {
                        successMessage({ type: data.type, data: data.data, info: data.info });
                    } else {
                        message.success(data.info);
                    }
                    break;
                case HTTP_CODE.ERROR:
                    if (!utils.empty(errorMessage)) {
                        errorMessage({ type: data.type, data: data.data, info: data.info });
                    } else {
                        message.error(data.info);
                    }
                    break;
                case HTTP_CODE.NOAUTH:
                    message.alert(data.info, { title: '没有权限' });
                    break;
                case HTTP_CODE.LOGIN:
                    message.alert(data.info, { title: "登录提示" });
                    break;
                case HTTP_CODE.RELOGIN:
                    message.alert(data.info, { title: "登录提示" });
                    break;
                default:
                    if (!utils.empty(errorMessage)) {
                        errorMessage("数据解析失败");
                    } else {
                        message.error("数据解析失败");
                    }
                    break;
            }
        };
    }
    /**
     * 获取状态
     */
    public GetReadyState(): number {
        if (utils.empty(this.socket)) {
            return 0;
        } else {
            return this.socket.readyState;
        }
    }
    /**
     * 获取状态名称
     */
    public GetReadyStateName(): string {
        let name = "";
        if (this.socket) {
            switch (this.socket.readyState) {
                case WebSocket.CONNECTING:
                    name = "正在连接……";
                    break;
                case WebSocket.OPEN:
                    name = "连接成功";
                    break;
                case WebSocket.CLOSING:
                    name = "正在关闭……";
                    break;
                case WebSocket.CLOSED:
                    if (this.socketType === "open") {
                        name = "连接失败";
                    } else if (this.socketType === "error") {
                        name = "断开连接";
                    } else if (this.socketType === "close") {
                        name = "连接已经关闭";
                    }
                    break;
                default:
                    name = "";
                    break;
            }
        }
        return name;
    }
    // 发送webSocket 消息
    public SendMessage(type: any, data: any) {
        this.socket.send(JSON.stringify({ type, data }));
    }
    /**
     * 关闭连接
     */
    public Close(): void {
        if (!utils.empty(this.socket)) {
            if (this.socket.readyState > 0) {
                this.socket.close();
            }
        }
    }
    /**
     * 获取状态类型
     */
    public GetSocketType(): string {
        return this.socketType;
    }
}
